package com.itheima.app.manager;

import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Voice;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.VideoService;
import com.itheima.service.mongo.VoiceService;
import com.itheima.vo.VoiceVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;

@Component
public class VoiceManager {

    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Autowired
    FdfsWebServer fdfsWebServer;

    @DubboReference
    VoiceService voiceService;
    @DubboReference
    UserInfoService userInfoService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //桃花传音-发送语音
    public void save(Long id, MultipartFile soundFile) throws IOException {
        //音频保存到fastDfs
        StorePath voicePath = fastFileStorageClient.uploadFile(
                soundFile.getInputStream(),
                soundFile.getSize(),
                FileUtil.extName(soundFile.getOriginalFilename()),
                null
        );
        //封装voice
        Voice voice = new Voice();
        voice.setCreated(System.currentTimeMillis());
        voice.setUserId(id);
        voice.setVoiceUrl(fdfsWebServer.getWebServerUrl()+voicePath.getFullPath());
        voiceService.saveVoice(voice);
    }

    //接收语音
    public ResponseEntity findVoice() {

        //获取当前用户id
        Long userId = UserHolder.get().getId();

        //防止查看的自己的
        Voice voice = new Voice();
        boolean b = true;
        while (b) {
            //调用service 查询随机voice
            voice = voiceService.findUserId();
            if (!userId.equals(voice.getUserId())) {
                b = false;
            }
        }
        //调用service查询userInfo
        UserInfo userInfo = userInfoService.findById(voice.getUserId());
        VoiceVo voiceVo = new VoiceVo();
        voiceVo.setId(userInfo.getId());
        voiceVo.setAvatar(userInfo.getAvatar());
        voiceVo.setNickname(userInfo.getNickname());
        voiceVo.setGender(userInfo.getGender());
        voiceVo.setAge(userInfo.getAge());
        voiceVo.setSoundUrl(voice.getVoiceUrl());
        //从redis获取剩余次数
        String surplus = stringRedisTemplate.opsForValue().get("videoSurplus_" + userId);
        if (surplus == null) {
            surplus = "7";
            stringRedisTemplate.opsForValue().set("videoSurplus_" + userId, surplus, Duration.ofDays(1));
        } else {
            Integer remainingTimes = Integer.valueOf(surplus) - 1;
            stringRedisTemplate.opsForValue().set("videoSurplus_" + userId, remainingTimes.toString());
        }
        Integer remainingTimes = Integer.valueOf(surplus);
        if (remainingTimes == 0) {
            return ResponseEntity.status(500).body("今日次数到了");
        }
        voiceVo.setRemainingTimes(remainingTimes);
        return ResponseEntity.ok(voiceVo);
    }
}